package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.platform.entity.Menu;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 平台基础菜单
 */
@Repository
public class MenuDao extends BaseDao<Menu> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 保存或者更新
     */
    public void saveOrUpdate(Menu menu){
        if(menu.getId()!=null && menu.getId() > 0){
            this.update(menu);
        }else{
            menu.setId(this.add(menu));
        }
    }
    //详情
    public Menu get(Integer id){
        if(StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select m.*,(select pm.title from bt_menu pm where pm.id = m.pid ) pname from bt_menu m  where 1=1");
        sbStr.append(" and m.id = ? ");
        paramList.add(id);
        sbStr.append(" ORDER BY sorts DESC ");
        String sql = sbStr.toString();
        return this.queryObj(sql,paramList, Menu.class);
    }

    //列表查询
    public List<Menu> findList(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select m.*,(select pm.title from bt_menu pm where pm.id = m.pid ) pname from bt_menu m  where 1=1");
        if(StringEx.isNotNull(paraMap.get("title"))){
            sbStr.append(" and m.title like ? ");
            paramList.add("%"+paraMap.get("title")+"%");
        }
        if(!StringEx.isNullOrLing(paraMap.get("states"))){
            sbStr.append(" and m.states = ? ");
            paramList.add(paraMap.get("states"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("pid"))){
            sbStr.append(" and m.pid = ? ");
            paramList.add(paraMap.get("pid"));
        }
        sbStr.append(" ORDER BY sorts DESC ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,Menu.class);
    }


    /**
     * 删除
     */
    public void del(int id){
        if (id <= 0) {
            return;
        }
        String sql = "delete  from bt_menu  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }
}
